#include "LoginPageWidget.h"
#include "ui_LoginPageWidget.h"

LoginPageWidget::LoginPageWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("client-login");
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);  // 隐藏最大化按钮
    resize(400, 300);
    setFixedSize(width(), height());


    setMinimumSize(width(), height());
    setMaximumSize(width(), height());

    this->setStyleSheet("background-color: #E0FFFF");

    ui->username->setStyleSheet(
         R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    ui->password->setStyleSheet(
        R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    ui->ip->setStyleSheet(
        R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    ui->port->setStyleSheet(
        R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );

    ui->port->setStyleSheet(
        R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );

    ui->btnlogin->setStyleSheet(
        R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );

    connect(&NetWorkMgr::instance(), &NetWorkMgr::LoginDone, this, &LoginPageWidget::LoginDone);

}

LoginPageWidget::~LoginPageWidget()
{
    delete ui;
}

void LoginPageWidget::on_btnlogin_clicked()
{
    //获取界面用户信息
    QString strusername = ui->username->text();
    QString strpassword = ui->password->text();
    QString strip = ui->ip->text();
    QString strport = ui->port->text();

    QJsonObject jsParam;
    jsParam["ip"] = strip;
    jsParam["port"] = strport;
    jsParam["username"] = strusername;
    jsParam["password"] = strpassword;
    //调用接口登陆
    NetWorkMgr::instance().Login(jsParam, "0");
}

void LoginPageWidget::LoginDone(const QJsonObject &jsResult)
{
   qDebug() << "LoginDone:";
    bool bsuccess = false;
    if(jsResult.contains("bsuccess"))
    {
        bsuccess = jsResult["bsuccess"].toBool();
    }
    if(bsuccess)
    {
        //成功
        qDebug() << "LoginSuccess:";
        emit LoginSuccess();

    }
    else
    {
        //失败
    }
}

